package com.nacos.shangtu.controller;

import com.nacos.shangtu.entity.po.UserPO;
import com.nacos.shangtu.entity.ro.GroupRo;
import com.nacos.shangtu.service.GroupService;
import com.nacos.shangtu.service.MessageService;
import com.nacos.shangtu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/im")
public class ImCustomerController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private UserService userService;

    @GetMapping("/messages")
    public ResponseEntity<Object> getMessageList(@RequestParam(value = "group_id") String groupId) {
        return new ResponseEntity<>(messageService.getMessageList(groupId), HttpStatus.OK);
    }

    @GetMapping("/conversions")
    public ResponseEntity<Object> getCoversations(@RequestParam(value = "user_id") String userId) {
        return new ResponseEntity<>(messageService.getConversions(userId), HttpStatus.OK);
    }

    @PostMapping("/create/group")
    public ResponseEntity<Object> createGroup(@RequestBody GroupRo groupRo) {
        String groupId = groupService.createGroup(groupRo.getOwnerId(),
                groupRo.getMembers(), groupRo.getGroupName());
        return ResponseEntity.ok(Map.of("groupId", groupId));
    }

    @PostMapping("/sync/user")
    public void syncUser(@RequestBody UserPO userPO) {
        userService.syncUser(userPO);
    }

}
